//
//  PicsCell.m
//  News_lzz
//
//  Created by qianfeng01 on 15-6-14.
//  Copyright (c) 2015年 李忠. All rights reserved.
//

#import "PicsCell.h"
#import <MediaPlayer/MediaPlayer.h>

@interface PicsCell ()
@property (strong, nonatomic) MPMoviePlayerViewController *mp;
@end

@implementation PicsCell

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    // Initialization code
    self.model = [[Model alloc] init];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (PicsCell *)setCellWithModel:(Model *)model {
    self.titleLabel.text = model.title;
    [self.kpic sd_setImageWithURL:[NSURL URLWithString:model.kpic] placeholderImage:nil];
    self.commentLabel.text = [model.comment stringValue];
    if (self.type) {
        self.model = model;
        [self.btn setImage:[UIImage imageNamed:@"video_cell_playIcon"] forState:UIControlStateNormal];
        self.startPlayer.hidden = NO;
    } else {
        self.startPlayer.hidden = YES;
        [self.btn setImage:[UIImage imageNamed:@"feed_cell_commentIcon.png"] forState:UIControlStateNormal];
    }
    return self;
}

- (IBAction)startPlayerClick:(id)sender {
    NSDictionary *videoDict = self.model.video_info;
    self.mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:videoDict[@"url"]]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [self.window.rootViewController presentViewController:self.mp animated:YES completion:nil];
    _mp.moviePlayer.shouldAutoplay = YES;
    [_mp.moviePlayer play];
}


- (void)playBack:(NSNotification *)nf{
    NSDictionary *dict = nf.userInfo;
    //获取播放返回的原因
    NSInteger type = [dict[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
    NSLog(@"%ld",type);
    //type 的值 1.自动播放完毕 2.点击Done 按钮  3.播放有异常
    switch (type) {
        case 0:
        {
            NSLog(@"自动播放完毕");
        }
            break;
        case 1:
        {
            [_mp.moviePlayer stop];
        }
            break;
        case 2:
        {
            NSLog(@"播放有异常");
        }
            break;
        default:
            break;
    }
    [self.mp dismissViewControllerAnimated:YES completion:nil];
}

@end
